#include "bencode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct rsschannel {
	char *title;
	char *link;
	char *description;
	char *language;
	struct rssitem **items;
	int itemCount;
} rsschannel;

typedef struct rssitem {
	char* title;
	char* link;
	char* description;
	char* pubDate;
	char* guid;
} rssitem;

char* filename;

void printRSSitem(rssitem* item) {
	/*
	 * Print the item in two table rows. 
	 * The first row will contain the title and the publish date.
	 * The second row the description with a link to the full article. 
	 */
	printf("\
		<tr>\n\
			<td>\n\
				<h1>%s</h1>\n\
			</td>\n\
			<td>\n\
				<span>Date published: %s</span>\n\
			</td>\n\
		</tr>\n\
		<tr>\n\
			<td colspan=\"2\"	>\n\
				<p>%s</p>\n\
				<a href=\"%s\">Read more...</a>\n\
			</td>\n\
		</tr>\n\
	",
	item->title,
	item->pubDate,
	item->description,
	item->link
	);
}

void printRSSchannel(struct rsschannel* channel) {
	int i = 0;
	/*
	 * Print the start of the HTML-file.
	 * Set the page title to the title of the RSS channel plus the language between brackets.
	 * Then show the title in the header of a table, referencing to the link provided in de RSS file.
	 * Next, print the description of the channel in a new row.
	 */ 
	printf("\
		<html>\n\
		<head>\n\
		<title>RSS: %s (%s)</title>\n\
		<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" />\n\
		</head>\n\
		<body>\n\
			<table>\n\
			<thead>\n\
				<tr>\n\
					<th colspan=\"2\">\n\
						<a href=\"%s\"><h1>%s</h1></a>\n\
					</th>\n\
				</tr>\n\
				<tr>\n\
					<th colspan=\"2\">\n\
						<p>\n%s\n</p>\n\
					</th>\n\
				</tr>\n\
			</thead>\n\
			<tbody>\n\
		",
		channel->title,
		channel->language,
		channel->link,
		channel->title,
		channel->description
	);
	/*
	 * Print the items of the channel
	 */
	for (i = 0; i < channel->itemCount; i++) {
		printRSSitem(channel->items[i]);
	}
	/*
	 * Close all HTML tags to finish it up
	 */
	printf("\
			</tbody>\n\
		</table>\n\
		</body>\n\
		</html>\n\
	");
}

struct rssitem* parseItem(be_dict *dictionary){
	struct rssitem *item;
	int i;
	
	// alloc space for the item

	item = malloc(sizeof(struct rssitem));

	// walk through all attributes of the be_node and put the data in the RSS item data structure
	for (i = 0; dictionary[i].val; ++i) {
		if(strcmp(dictionary[i].key,"title")==0){
			item->title=dictionary[i].val->val.s;
		}else if(strcmp(dictionary[i].key,"link")==0){
			item->link=dictionary[i].val->val.s;
		}else if(strcmp(dictionary[i].key,"description")==0){
			item->description=dictionary[i].val->val.s;
		}else if(strcmp(dictionary[i].key,"pubDate")==0){
			item->pubDate=dictionary[i].val->val.s;
		}else if(strcmp(dictionary[i].key,"guid")==0){
			item->guid=dictionary[i].val->val.s;
		}
	}
	
	return item;
}

struct rsschannel* readFileToRSS(char* filename){
	FILE *fp;
	int size;
	
	// open the file
	fp = fopen(filename, "r");
	
	if (fp == NULL){
		printf("File %s doesn't exists!\n", filename);
		exit(0);
	}
	
	// seek the end of the file
	fseek(fp, 0, SEEK_END); 
	// get the current pointer in the file: THE END :)

	size = ftell(fp)/sizeof(char); 
	// set pointer back to the start of the file

	fseek(fp, 0, SEEK_SET); 
	
	// allocate a buffer to put the file in
	char buffer[size];
	// define some variables
	be_node* channel;
	struct rsschannel *rss;
	int i;
	
	// place the file in the buffer
	fgets(buffer, size, fp);

	// convert the buffercontents to a be_node
	channel = be_decode(buffer);
	// alloc memory for the RSS data structure
	rss = malloc(sizeof(struct rsschannel));

	// check the channel to be of type dictionary
	if (channel->type!=BE_DICT) {
		printf("Channel needs to be dictionary!"); 
		exit(0); 
	}

	// initialize item-values
	rss->itemCount = 0;
	rss->items = NULL;

	// walk through all attributes of the be_node and put the data in the RSS channel data structure
	for (i = 0; channel->val.d[i].val; ++i) {
		if(strcmp(channel->val.d[i].key, "title")==0){
			rss->title = channel->val.d[i].val->val.s;
		} else if(strcmp(channel->val.d[i].key, "link")==0){
			rss->link = channel->val.d[i].val->val.s;
		} else if(strcmp(channel->val.d[i].key, "description")==0){
			rss->description = channel->val.d[i].val->val.s;
		} else if(strcmp(channel->val.d[i].key, "language")==0){
			rss->language = channel->val.d[i].val->val.s;
		} else if(strncmp(channel->val.d[i].key, "item", 4)==0){
			rss->itemCount++;
			// realloc the items list
			rss->items = realloc(rss->items, rss->itemCount * sizeof(rssitem*));
			// parse the item and place it in the list of items
			rss->items[rss->itemCount - 1] = parseItem(channel->val.d[i].val->val.d);
		}
	}




	// close the file







	fclose(fp);
	
	return rss;
}

int main(int argc, char *argv[]){
	struct rsschannel* channel;
	if (argc==1){
		printf("Usage: ./as3_t2 <filename>\n");
		exit(0);
	} else{
		// get the file name
		filename = argv[1];
		// get the contents of the file converted to RSS data structure
		channel = readFileToRSS(filename);
		// print it as HTML
		printRSSchannel(channel);



	}
	
	return 0;
}




